home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_110 / mint / bash110s.zoo / bash-1.10 / builtins / read.def < prev    next >
Encoding:
Text File  |  1991-09-19  |  5.0 KB  |  198 lines

  1. This file is read.def, from which is created read.c.
  2. It implements the builtin "read" in Bash.
  3.  
  4. Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  5.  
  6. This file is part of GNU Bash, the Bourne Again SHell.
  7.  
  8. Bash is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 1, or (at your option) any later
  11. version.
  12.  
  13. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License along
  19. with Bash; see the file COPYING.  If not, write to the Free Software
  20. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. $PRODUCES read.c
  23.  
  24. $BUILTIN read
  25. $FUNCTION read_builtin
  26. $SHORT_DOC read [-r] [name ...]
  27. One line is read from the standard input, and the first word is
  28. assigned to the first NAME, the second word to the second NAME, etc.
  29. with leftover words assigned to the last NAME.  Only the characters
  30. found in $IFS are recognized as word delimiters.  The return code is
  31. zero, unless end-of-file is encountered.  If the -r option is given, a
  32. backslash-newline pair (\\n) is not ignored, and the backslash is
  33. considered to be part of the line.
  34. $END
  35.  
  36. #include <stdio.h>
  37. #include "../shell.h"
  38.  
  39. static int stream_close ();
  40.  
  41. /* Read the value of the shell variables whose names follow.
  42.    The reading is done from the current input stream, whatever
  43.    that may be.  Successive words of the input line are assigned
  44.    to the variables mentioned in LIST.  The last variable in LIST
  45.    gets the remainder of the words on the line.  If no variables
  46.    are mentioned in LIST, then the default variable is $REPLY.
  47.  
  48.    S. R. Bourne's shell complains if you don't name a variable
  49.    to receive the stuff that is read.  GNU's shell doesn't.  This
  50.    allows you to let the user type random things. */
  51. read_builtin (list)
  52.      WORD_LIST *list;
  53. {
  54.   extern int interrupt_immediately, free ();
  55.   extern SHELL_VAR *bind_variable ();
  56.   register char *varname;
  57.   int size, c, i = 0, fildes, ignore_backslash_nl = 1;
  58.   char *input_string, *ifs_chars;
  59.   WORD_LIST *words, *rwords, *list_string ();
  60.   extern char *string_list_dollar_star ();
  61.   FILE *input_stream;
  62.  
  63.   ifs_chars = get_string_value ("IFS");
  64.   input_string = (char *)xmalloc (size = 128);
  65.  
  66.   /* We need unbuffered input from stdin.  So we make a new
  67.      unbuffered stream with the same file descriptor, then
  68.      unbuffer that one. */
  69.   fildes = dup (fileno (stdin));
  70.  
  71.   if (fildes == -1)
  72.     return (EXECUTION_FAILURE);
  73.  
  74.   input_stream = fdopen (fildes, "r");
  75.  
  76.   if (!input_stream)
  77.     {
  78.       close (fildes);
  79.       return (EXECUTION_FAILURE);
  80.     }
  81.  
  82.   setbuf (input_stream, (char *)NULL);
  83.  
  84.   {
  85.     begin_unwind_frame ("read_builtin");
  86.     add_unwind_protect (free, input_string);
  87.     add_unwind_protect (stream_close, input_stream);
  88.     interrupt_immediately++;
  89.   }
  90.  
  91.   while (list)
  92.     {
  93.       if (strcmp (list->word->word, "-r") == 0)
  94.     {
  95.       ignore_backslash_nl = 0;
  96.       list = list->next;
  97.     }
  98.       else if (strcmp (list->word->word, "--") == 0)
  99.     {
  100.       list = list->next;
  101.       break;
  102.     }
  103.       else if (*list->word->word == '-')
  104.     {
  105.       bad_option (list->word->word);
  106.       return (EXECUTION_FAILURE);
  107.     }
  108.       else
  109.     break;
  110.     }
  111.  
  112.   while ((c = getc (input_stream)) != EOF)
  113.     {
  114.       if (i + 1 >= size)
  115.     input_string = (char *)xrealloc (input_string, size += 128);
  116.  
  117.       input_string[i++] = c;
  118.  
  119.       if (c == '\n')
  120.     {
  121.       if (ignore_backslash_nl &&
  122.           (i >= 2) && (input_string[i - 2] == '\\'))
  123.         {
  124.           i -= 2;
  125.           continue;
  126.         }
  127.       /* Both ksh and the s5r3 sh chop off the trailing newline. */
  128.       i--;
  129.       break;
  130.     }
  131.     }
  132.   input_string[i] = '\0';
  133.  
  134.   interrupt_immediately--;
  135.   discard_unwind_frame ("read_builtin");
  136.  
  137.   fclose (input_stream);
  138.  
  139.   if (c == EOF)
  140.     return (EXECUTION_FAILURE);
  141.  
  142.   if (!list)
  143.     {
  144.       SHELL_VAR *var;
  145.  
  146.       var = bind_variable ("REPLY", input_string);
  147.       var->attributes &= ~att_invisible;
  148.       free (input_string);
  149.     }
  150.   else
  151.     {
  152.       words = list_string (input_string, ifs_chars, 0);
  153.       rwords = words;
  154.  
  155.       free (input_string);
  156.  
  157.       while (list)
  158.     {
  159.       SHELL_VAR *var;
  160.  
  161.       varname = list->word->word;
  162.  
  163.       if (!list->next)
  164.         {
  165.           /* Call string_list_dollar_star because P1003.2.9 specifies
  166.          that the intervening separators are preserved in the
  167.          result of a read that specifies fewer variables than words
  168.          read. */
  169.           char *t = words ? string_list_dollar_star (words) : "";
  170.           var = bind_variable (varname, t);
  171.         }
  172.       else
  173.         var = bind_variable (varname, words ? words->word->word : "");
  174.  
  175.       stupidly_hack_special_variables (varname);
  176.       var->attributes &= ~att_invisible;
  177.  
  178.       list = list->next;
  179.       if (words)
  180.         words = words->next;
  181.     }
  182.       if (rwords)
  183.     dispose_words (rwords);
  184.     }
  185.  
  186.   return (EXECUTION_SUCCESS);
  187. }
  188.  
  189. /* This way I don't have to know whether fclose () is a
  190.    function or a macro. */
  191. static int
  192. stream_close (file)
  193.      FILE *file;
  194. {
  195.   return (fclose (file));
  196. }
  197.  
  198.